#!/bin/bash
#(c) Copyright 2009 Barry Kauler.
#120126 mavrothal: offer option to skip specific repos. refer http://bkhome.org/blog/?viewDetailed=02650
#120315 pkg list entries may have optional forced repo field. Ref: http://bkhome.org/blog/?viewDetailed=02414
#120515 build from "gentoo" binary tarballs (refer support/gentoo). --no need to modify anything in 0setup.
#120719 support raspbian. fix debian db format change.
#120804 fix when only have pkgname_DEV, no pkgname.
#120812 db category now allows optional subcategory (for which an icons exists in /usr/local/lib/X11/mini-icons).
#120815 for debian, ubuntu, use the generic name provided in the $DB_path, instead of $Package, for find_cat.
#120825 debian/ubuntu/raspbian: merge -updates dbs.
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#121111 debdb2pupdb, new fast deb to pup db converter.
#121112 note, debdb2pupdb no longer calls find_cat.
#121113 improve file PKGS_HOMEPAGES sort after run debdb2pupdb.
#121113 read Ubuntu-db 'Section' parameter to help assign category.
#121113 ubuntu-db: introduce STARTMARKER field between db records.
#121113 reverse question, enter only for ubuntu db -updates.
#121116 0setup also runs in running puppy called from ppm. some o/p needs to be translated, done in /usr/share/sss/script_strings/script_strings. see below all LANGORG insertions.
#130126 find_cat rewritten. works as before, also can post-process an entire db-entry or db-entries-file. arch code rewritten by noryb009
#130305 debdb2pupdb.bac now also check that deps exist, create list of pkgs.
#130316 debdb2pupdb does not use /tmp/0setupcompletelistpkgs in a running puppy.
#130319 remove 'raspbian' from updates code section.

LANGORG="$(locale | grep '^LANG=' | cut -d '=' -f 2)" #121116
export LANG=C #faster.

if [ "$SETUPCALLEDFROM" = "ppm" ];then
 DBmethod="$(cat /var/local/petget/db_verbose 2>/dev/null)"
 [ "$DBmethod" = "" ] && DBmethod=true
 if [ "$DBmethod" = "false" ]; then
  LANG=${LANGORG} . yaf-splash -icon gtk-info -bg white -placement top -close never -text "$(gettext 'Updating package databases.
Please wait...')" &
  XPID=$!
 fi
else
 DBmethod=true
fi

if [ -f ./DISTRO_SPECS ];then
  . ./DISTRO_SPECS
  . ./_00func
  source_compat_repos
  source_pkgs_specs
  . ./PKGS_MANAGEMENT
  . ./DISTRO_PET_REPOS
  RUNNINGPUP='no'
  # 140109 64 bit support #140116 improve
  . ./WOOFMERGEVARS
  if [ "${WOOF_COMPATDISTRO}" = "slackware64" ];then
    # see if it's been done
    LIB64DONE=`find packages-templates -type d -name 'lib64'`
    if [ "$LIB64DONE" ];then
      echo "lib64 directories in packages-templates already exist."
    else
      LIST=`ls packages-templates|grep -v pcmciautils`
      echo -n "fixing package templates for 64 bit: "
      sleep 2 #time to read
      for i in $LIST
      do
        echo -n "$i "
        if [ -d packages-templates/${i}/lib ];then
          mv packages-templates/${i}/lib packages-templates/${i}/lib64
          echo -n '' > packages-templates/${i}/PLUSEXTRADIRS
        fi
        [ -d packages-templates/${i}/usr/lib ] \
        && mv packages-templates/${i}/usr/lib packages-templates/${i}/usr/lib64
      done
      echo -e "\n\nlib64 directories created in packages-templates."
    fi
  fi
  DEBDB2PUPDB="$(pwd)/support/debdb2pupdb"
  FIND_CAT="$(pwd)/support/find_cat"
  #note, 3builddistro copies it into rootfs-complete/usr/local/petget when building a pup.
else
  #want to run this script in running puppy, to update db's...
  DEBDB2PUPDB='/usr/local/petget/debdb2pupdb'
  FIND_CAT='/usr/local/petget/find_cat'
  . /etc/DISTRO_SPECS
  . /root/.packages/DISTRO_COMPAT_REPOS #v431
  . /root/.packages/DISTRO_PKGS_SPECS
  . /root/.packages/PKGS_MANAGEMENT
  . /root/.packages/DISTRO_PET_REPOS
  cd /root/.packages
  RUNNINGPUP='yes'
fi

#remove comments from PKGS_SPECS_TABLE
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#'`"

#======================================================================

#w008 convert pkg database ${PKGLISTFILE}pre to a standard format...
#120812 subcategory...
#each line: pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|
#ex: abiword-1.2.4|abiword|1.2.4|5|Document|999K|slackware/ab|abiword-1.2.4-5-i486.tgz|+aiksausus,+gtk2|a nice wordprocessor|
PKGLISTS_COMPAT="`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|' | tr '\n' ' '`" #see file DISTRO_PKGS_SPECS-ubuntu

function check_compat_distro_pkgs() {
	####################################
	#extract all the package names of compat-distro, without version info...
	#                 *this*
	#automake_1.14.1|automake|1.14.1|2ubuntu1|Filesystem;filemanager|1492K|pool/main/a/automake-1.14|automake_1.14.1-2ubuntu1_all.deb|+autoconf&ge2.65,+autotools-dev&ge20020320.1|Tool for generating GNU Standards-compliant Makefiles|ubuntu|trusty||
	cat $PKGLISTS_COMPAT | cut -f 2 -d '|' > zz_pkgnamespre
	#in debian/ubuntu there are too many lib* packages
	grep '^lib' zz_pkgnamespre > zz_pkgnames
	grep -v '^lib' zz_pkgnamespre >> zz_pkgnames

	#check the entries in PKGS_SPECS_TABLE against those in Packages-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}.
	#that is, need to verify that pkgs specified in table do actually exist.
	echo -e "\nChecking that compat-distro pkgs specified in PKGS_SPECS_TABLE actually exist..." | tee /var/woof/0setup_fail_report_$RUNNINGPUP
	FAILCHK="no"

	#120315 extra field on end to force repo... grep -v '||[a-zA-Z0-9]*:' (not actually needed)
	echo "$PKGS_SPECS_TABLE" | grep '^yes' | grep -v '|pet:' | cut -f 3,4 -d '|' > zz_fields34
	#yes|alsa-lib|libasound2,libasound2-data,libasound2-dev,libasound2-plugins|exe,dev,doc,nls
	#yes|advancecomp|advancecomp|exe>dev,dev,doc,nls
	#yes|abiword||exe,dev,doc,nls
	while read AFIELD34
	do
	 #              BINARYPARTNAMES  ASSIGNFIELD
	 #yes|advancecomp|advancecomp|exe>dev,dev,doc,nls
	 IFS="|" read -r FPARTNAMES ASSIGNFIELD <<< "$AFIELD34"
	 [ ! "$FPARTNAMES" ] && continue
	 BINARYPARTNAMES=""
	 for I in ${FPARTNAMES//,/ } ; do
	   case $I in -*) continue ;; esac #ignore -pkgs
	   BINARYPARTNAMES+=" $I"
	 done
	 for ONEBINARYPARTNAME in $BINARYPARTNAMES
	 do
	  #ONEBINARYPARTNAME may match multiple pkgs...
	  #echo -n " $ONEBINARYPARTNAME"
	  multiPATTERN="${ONEBINARYPARTNAME//-/\\-}" #replace "-" with "\-"
	  multiPATTERN="${multiPATTERN//\*/.*}"      #replace "*" with ".*"
	  grep -m 1 -q "^${multiPATTERN}$" zz_pkgnames && continue #success.

	  if [ "$ASSIGNFIELD" ];then #120804
	   #this will find entry in PKGS_SPECS_TABLE like 'yes|apr|apr|exe>dev,dev,doc,nls', where pkg apr-123 does not exist, only apr_DEV-123...
	   case "$ASSIGNFIELD" in *'exe>dev'*|*'exe>null'*)
		grep -m 1 -q "^${ONEBINARYPARTNAME}_DEV" zz_pkgnames && continue #success.
	   esac
	   #this entry 'yes|perl-digest-sha1|perl-digest-sha1|exe,dev>null,doc>null,nls>null' also only has a _DEV pkg...
	   case "$ASSIGNFIELD" in *'dev>exe'*) 
		grep -m 1 -q "^${ONEBINARYPARTNAME}_DEV" zz_pkgnames && continue #success.
	   esac
	  fi
	  case "$ONEBINARYPARTNAME" in *"*"*)
	   grep -m 1 -q "^${multiPATTERN}" zz_pkgnames && continue #success.
	  esac
	  #failure...
	  echo "FAIL: $ONEBINARYPARTNAME"
	  echo "FAIL: $ONEBINARYPARTNAME" >> /var/woof/0setup_fail_report_$RUNNINGPUP
	  FAILCHK="yes"
	 done
	done < zz_fields34

	rm -f zz_fields34  #comment out to debug
	rm -f zz_pkgnames* #--

	if [ "$FAILCHK" = "yes" ];then
	 echo "
Finished, but some failures. You need to fix the above entries in variable
PKGS_SPECS_TABLE in file ${DPSFILE}.
Search for suitable packages in these files:
`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|'`
Or, search online. In the case of Debian or Ubuntu, you can search
at packages.debian.org or packages.ubuntu.com.
After you have fixed PKGS_SPECS_TABLE, run this script again." | tee -a /var/woof/0setup_fail_report_$RUNNINGPUP
	 if [ "$DBmethod" = "false" ];then
	  kill $XPID
	  Xdialog --title "$(gettext 'PPM database')" --left --msgbox "$(gettext 'PPM database failed. Chech /var/woof/0setup_fail_report_')$RUNNINGPUP $(gettext 'for details.')" 0 100
	 fi
	 unset OSETUP_CALLED_FROM
	else
	 echo -e "\nFinished, successful." | tee -a /var/woof/0setup_fail_report_$RUNNINGPUP
	fi

	if [ "$DBmethod" = "true" ]; then
	 echo
	 echo "The log of '0setup', as reported above, is saved for future reference."
	 echo "(In file /var/woof/0setup_fail_report_${RUNNINGPUP})"
	 echo "In the Woof GUI, tab 'Download dbs', click the 'REPORT' button to"
	 echo "retrieve this log."
	 echo -e "\nScript finished."
	 [ "$WOOF_GUI" ] && { echo -n "Press ENTER key to exit: " ; read zzz ; }
	else
	 kill $XPID
	 if [ "$(grep -e 'FAIL|exited|failures' /var/woof/0setup_fail_report_$RUNNINGPUP)" = "" ]; then
	  Xdialog --title "$(gettext 'PPM database')" --left --msgbox "$(gettext 'PPM databases update was successfully completed.')" 0 100
	 else
	  Xdialog --title "$(gettext 'PPM database')" --left --msgbox "$(gettext 'PPM databases update failed. Chech /var/woof/0setup_fail_report_')$RUNNINGPUP $(gettext 'for details.')" 0 100
	 fi
	fi
	unset OSETUP_CALLED_FROM
}

case $1 in check|-check)
	check_compat_distro_pkgs
	exit
esac

#======================================================================

#w480 check puppy pkg db files...
if [ "$RUNNINGPUP" = "no" ];then
 ./support/fix-puppy-dbs
 [ $? -eq 1 ] && exit
fi
rm -f remotedbfile #created by fix-puppy-dbs

#w091018 now logging errors for future recall...
mkdir -p /var/woof
RUNDATE="`date`"
echo -n "" > /var/woof/0setup_fail_report_$RUNNINGPUP #RUNNINGPUP=yes or no. latter if woof.
LANG=${LANGORG} echo "This is a report on the last time the '0setup' script was run.
Date and time '0setup' last run: ${RUNDATE}
Compatible-distro and release-name: ${DISTRO_BINARY_COMPAT}, ${DISTRO_COMPAT_VERSION}
Mostly only errors get logged, so the less seen below, the better.
Log of last run of '0setup':
" >> /var/woof/0setup_fail_report_$RUNNINGPUP

function pkg_db_confirm_download() {
  downloadit=""
  if [ "$DBmethod" = "true" ];then
   echo
   LANG=${LANGORG} echo "A package information database file needs to be downloaded:"
   echo "> $PKGLISTFILE"
   LANG=${LANGORG} echo "This will be downloaded from:"
   echo "> $PKGLISTURI"
   if [ "$RUNNINGPUP" = "yes" ];then
     LANG=${LANGORG} echo "and will be processed and named: ${PKGLISTFILE}"
     LANG=${LANGORG} echo -n "Press ENTER key to download, any other to skip it: " #120126
     read downloadit
   fi
  fi
  [ "$downloadit" = "" ] && return 0 || return 1
}

#download docs on compatible-distro pkgs...
function download_compat_pkg_dbs() {
	for PKGLISTSPEC in "$@" #see file DISTRO_PKGS_SPECS-ubuntu
	do
	 PKGLISTFILE="`echo -n "$PKGLISTSPEC" | cut -f 3 -d '|'`"
	 PKGLISTURI="`echo -n "$PKGLISTSPEC" | cut -f 2 -d '|'`"
	 [ "$PKGLISTURI" = "" ] && continue #w478 no url, pkg db file only kept in woof.
	 skipdl=""
	 if [ "$RUNNINGPUP" = "no" -a -f $PKGLISTFILE -a "$DBmethod" = "true" ];then
	  echo
	  echo "Local db file '${PKGLISTFILE}' already exists."
	  echo "Press ENTER key only to upgrade it,"
	  echo -n "or any other printable char to skip: "
	  read skipdl
	 fi
	 if [ "$skipdl" = "" ];then 
	  if pkg_db_confirm_download ; then
	   DLFILE="`basename $PKGLISTURI`"
	   [ -f $DLFILE ] && mv -f $DLFILE /tmp/${DLFILE}-backup1 #v431 otherwise wget creates a new file ${DLFILE}.1
	   [ "$DBmethod" = "true" ] && rxvt -name pet -bg orange -geometry 80x10 -e wget $PKGLISTURI || wget $PKGLISTURI
	   sync
	   xDLFILE="$DLFILE"
	   if [ -f $DLFILE ];then
		case ${DISTRO_BINARY_COMPAT} in
		 ubuntu|trisquel|debian|devuan|raspbian)
		  xDLFILE="`basename $DLFILE`"
		  xDLFILE=${xDLFILE%.*}
		  rm -f $xDLFILE
		  case "$DLFILE" in
		   *.gz)  gunzip  $DLFILE ; RETSTAT=$? ;;
		   *.bz2) bunzip2 $DLFILE ; RETSTAT=$? ;;
		   *.xz)  unxz    $DLFILE ; RETSTAT=$? ;;
		  esac
		  [ $RETSTAT -eq 0 ] && mv -f $xDLFILE ${PKGLISTFILE}pre
		 ;;
		 slackware*) RETSTAT=0 ; mv -f $xDLFILE ${PKGLISTFILE}pre ;;
		 puppy) RETSTAT=0 ;;
		esac
		if [ $RETSTAT -eq 0 ];then
		 LANG=${LANGORG} echo "...success."
		else
		 LANG=${LANGORG} echo "Downloaded file is corrupted. Deleting file and aborting."
		 rm -f $DLFILE $xDLFILE 2>/dev/null
		 if [ "$DBmethod" = "false" ];then
		  kill $XPID
		  Xdialog --title "$(gettext 'PPM database')" --left --msgbox "$(gettext 'PPM database failed. Chech /var/woof/0setup_fail_report_')$RUNNINGPUP $(gettext 'for details.')" 0 100
		 fi
		 unset OSETUP_CALLED_FROM
		 exit
		fi
	   else #file not found
		LANG=${LANGORG} echo "Failed to download, aborting."
		LANG=${LANGORG} echo "
	Failed to download db file: 
	 ${PKGLISTURI}
	...exited from 0setup script." >> /var/woof/0setup_fail_report_$RUNNINGPUP
		if [ "$DBmethod" = "false" ];then
		 kill $XPID
		 Xdialog --title "$(gettext 'PPM database')" --left --msgbox "$(gettext 'PPM database failed. Chech /var/woof/0setup_fail_report_')$RUNNINGPUP $(gettext 'for details.')" 0 100
		fi
		unset OSETUP_CALLED_FROM
		exit
	   fi
	  else #120126
	   LANG=${LANGORG} echo "...skipped"
	  fi
	 fi
	done
}

###download pet pkg databases...
for PKGLISTSPEC in $PKG_DOCS_PET_REPOS  #see file DISTRO_PET_REPOS
do
 PKGLISTURI="`echo -n "$PKGLISTSPEC" | cut -f 2 -d '|'`"
 PKGLISTFILE="${PKGLISTURI##*/}" #basename
 if [ "$RUNNINGPUP" = "yes" -o ! -f $PKGLISTFILE ];then
  if pkg_db_confirm_download ; then
   DLFILE=${PKGLISTFILE}
   [ -f $DLFILE ] && mv -f $DLFILE /tmp/${DLFILE}-backup1 #v431 otherwise wget creates a new file ${DLFILE}.1 111129 prevent error msg.
   [ "$DBmethod" = "true" ] && rxvt -name pet -bg orange -geometry 80x10 -e wget $PKGLISTURI || wget $PKGLISTURI
   sync
   [ -f $DLFILE ] && LANG=${LANGORG} echo "...success"
  else #120126
   LANG=${LANGORG} echo "...skipped"
  fi
 fi
done

download_compat_pkg_dbs $PKG_DOCS_DISTRO_COMPAT #---

# -update dbs... #${DISTRO_BINARY_COMPAT}-updates package databases
PKGLISTS_COMPAT_UPDATES=""
case $DISTRO_BINARY_COMPAT in ubuntu|trisquel|debian|devuan) #130319 removed |raspbian
   DBUPDATEFLAG='yes'
   #Packages-ubuntu-precise-main -> Packages-ubuntu-precise_updates-main
   #http://archive.ubuntu.com/ubuntu/dists/precise/main/binary-i386/Packages.bz2 -> http://archive.ubuntu.com/ubuntu/dists/precise-updates/main/binary-i386/Packages.bz2
   PKGLISTS_COMPAT_U=$(echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | grep -v 'deb-multimedia' | \
     sed -e "s|-${DISTRO_COMPAT_VERSION}-|-${DISTRO_COMPAT_VERSION}_updates-|" \
         -e "s|/${DISTRO_COMPAT_VERSION}/|/${DISTRO_COMPAT_VERSION}-updates/|")
   download_compat_pkg_dbs $PKGLISTS_COMPAT_U
   #see further down
   PKGLISTS_COMPAT_UPDATES=$(echo "$PKGLISTS_COMPAT_U" | cut -f 3 -d "|" | tr '\n' ' ')
esac

case $DISTRO_BINARY_COMPAT in slackware*) # slackware official updates = patches repo
   DBUPDATEFLAG='yes'
   PKGLISTS_COMPAT_U=$(echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | grep official | \
    sed -e "s|-official|_updates-official|" \
        -e "s|/PACKAGES.TXT|/patches/PACKAGES.TXT|")
   download_compat_pkg_dbs $PKGLISTS_COMPAT_U
   PKGLISTS_COMPAT_UPDATES=$(echo "$PKGLISTS_COMPAT_U" | cut -f 3 -d "|" | tr '\n' ' ')
esac

#130305 debdb2pupdb.bac now also check that deps exist, need this list of pkgs...
if [ "$RUNNINGPUP" = "no" ];then #130316 debdb2pupdb does not read this file in a running pup.
 case ${DISTRO_BINARY_COMPAT} in
  ubuntu|trisquel|debian|devuan|raspbian)
   echo -n " " > /tmp/0setupcompletelistpkgs
   COMPLETELISTPKGS="$(grep '^Package: ' Packages*pre | cut -f 2 -d ' ' | tr '\n' ' ')"
   echo "$COMPLETELISTPKGS" >> /tmp/0setupcompletelistpkgs
   [ -f /tmp/0setupnewinvaliddeps ] && rm -f /tmp/0setupnewinvaliddeps #see debdb2pupdb.
  ;;
 esac
else
 [ -f /tmp/0setupcompletelistpkgs ] && rm -f /tmp/0setupcompletelistpkgs
fi
echo

#...format ex: 'Packages-ubuntu-intrepid-main Packages-ubuntu-intrepid-universe'
for ONE_PKGLISTS_COMPAT in $PKGLISTS_COMPAT $PKGLISTS_COMPAT_UPDATES #120825
do
 [ ! -f ${ONE_PKGLISTS_COMPAT}pre ] && continue
 LANG=${LANGORG} echo "Processing ${ONE_PKGLISTS_COMPAT} into a standard format..."
 echo -n "" > $ONE_PKGLISTS_COMPAT
 echo -n "" > /tmp/${ONE_PKGLISTS_COMPAT}temp #130126
 case ${DISTRO_BINARY_COMPAT} in
  ubuntu|trisquel|debian|devuan|raspbian)
    #new fast compiled app...
    [ ! -f $DEBDB2PUPDB ] && { echo "FATAL: Could not find $DEBDB2PUPDB" ; exit 1 ; }
    #create an intermediate-converted file...
    #121113 added "Section"... 121113 insert STARTMARKER...
    MANIPULATED1="$(grep -E '^Package:|^Installed\-Size:|^Architecture:|^Version:|^Depends:|^Filename:|^Description:|^Homepage:|^Section:' ${ONE_PKGLISTS_COMPAT}pre | tr '[\t"|`]' ' ' | tr -s ' ' | sed -e 's%^Installed\-Size%InstalledSize%' -e 's%^Package:%STARTMARKER|PKG\nPackage:%' -e 's%: %|%' -e 's% $%%')" #`geany
    #call new app. note, this in turn calls /usr/local/petget/find_pkgs...
    if [ -n "$MANIPULATED1" ];then
     echo "$MANIPULATED1" > /tmp/woof-debdb.in
     ${DEBDB2PUPDB} ${DISTRO_BINARY_COMPAT} ${DISTRO_COMPAT_VERSION} > /tmp/${ONE_PKGLISTS_COMPAT}temp #130126 $ONE_PKGLISTS_COMPAT
     ${FIND_CAT} /tmp/${ONE_PKGLISTS_COMPAT}temp > $ONE_PKGLISTS_COMPAT #130126
    else
     touch $ONE_PKGLISTS_COMPAT
    fi
    #homepages also get logged...
    if [ -f /tmp/woof-homepages.acc -a -f ./PKGS_HOMEPAGES ];then #running in woof.
      cat  ./PKGS_HOMEPAGES >> /tmp/woof-homepages.acc
      sort --key=1,1 --field-separator=" " --unique /tmp/woof-homepages.acc > ./PKGS_HOMEPAGES #121113
    fi
    #130316 /tmp/0setupnewinvaliddeps is created by debdb2pupdb, when run in woof.
    # 3builddistro will have to copy this file 'invaliddepslist' into rootfs-complete/usr/local/petget/ ...
    # it is read by debdb2pupdb in a running puppy.
    if [ "$RUNNINGPUP" = "no" ];then
     echo -n ' ' > invaliddepslist
     NIDL1="$(sort -u /tmp/0setupnewinvaliddeps 2>/dev/null | tr '\n' ' ')"
     echo -n "$NIDL1" >> invaliddepslist
    fi
  ;;
  slackware*)
   DB_category="" #130126 #151221 rewrote this case to use mostly shell - 150% speed improvement
   #note stabellini site has PACKAGES.TXT for the official 12.2 repo, with dependency info. #using salix
   #however, there is an extra 'PACKAGE MIRROR' field so i have added this preprocess line...
   #some pipe (|) chars screw up dep info so replace with commas in the pre process #01micko 20110505
   #sed -i 's%|%,%g' ${ONE_PKGLISTS_COMPAT}pre
   cat ${ONE_PKGLISTS_COMPAT}pre | grep -v '^PACKAGE MIRROR' | tr '\t' ' ' | tr -s ' ' | sed -e 's% $%%' -e 's%  %\n%g' | \
   sed -e 's%^PACKAGE %%' -e 's%SIZE (compressed)%SIZE_COMP%' -e 's%SIZE (uncompressed)%SIZE_NOCOMP%' > ${ONE_PKGLISTS_COMPAT}pre2
    (
    while read -r field desc ; do
      case ${field%:} in
        "NAME")
          DB_fullfilename="$desc"
          DB_pkgname="${DB_fullfilename%-*}"
          DB_pkgname="${DB_pkgname%-*}"
          ;;
        "LOCATION") DB_path="${desc#*/}" ;;
        #"SIZE_COMP") ;;
        "SIZE_NOCOMP") DB_size="${desc// /}" ;;
        "REQUIRED")
          DB_dependencies="$desc"
          [ "$DB_dependencies" != "" ] && DB_dependencies='+'"${DB_dependencies}"
          [ "$DB_dependencies" != "" ] && DB_dependencies="${DB_dependencies//|/,}"
          #...processed to ex: +acl,+gcc,+glibc-solibs,+libXt,+libart_lgpl
          [ "$DB_dependencies" != "" ] && DB_dependencies="${DB_dependencies//,/,+}"
          [ "$DB_dependencies" = "+PACKAGE" ] &&  DB_dependencies='' #01micko 20110504 bug with "+PACKAGES" sneaking through
          case "$DB_dependencies" in *">"*|*"="*|*"<"*) #slacky: pkg >= version
            DB_dependencies=$(echo "$DB_dependencies" | tr ',' '\n' | sed -e 's| .*||' | tr '\n' ',')
          esac
          ;;
        #"CONFLICTS") ;;
        #"SUGGESTS") ;;
        "DESCRIPTION")
          read -r field desc
          DB_nameonly="${field%:}"
          #-
          DB_version=${DB_pkgname#${DB_nameonly}-}
          DB_pkgrelease=${DB_fullfilename##*-}
          DB_pkgrelease=${DB_pkgrelease%.t*} #slackware has a consistent format of pkgname-version-arch-tag.t?z
          #-
          DB_description="${desc#*(}"
          DB_description="${DB_description%)*}"
          DB_description="${DB_description//,/}"
          [ -z "$DB_description" ] && DB_description="no description provided"
          #-
          echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|$DISTRO_BINARY_COMPAT|$DISTRO_COMPAT_VERSION||" #>> /tmp/${ONE_PKGLISTS_COMPAT}temp #130126 $ONE_PKGLISTS_COMPAT
          while [ 1 ] ; do read -r zzz ; [ ! "$zzz" ] && break ; done
          ;;
      esac
    done < ${ONE_PKGLISTS_COMPAT}pre2
    ) > /tmp/${ONE_PKGLISTS_COMPAT}temp
    sed -i -e 's%,|%|%' -e 's%,+|%|%' /tmp/${ONE_PKGLISTS_COMPAT}temp #slacky
    rm -f ${ONE_PKGLISTS_COMPAT}pre2
   #get the category...
   ${FIND_CAT} /tmp/${ONE_PKGLISTS_COMPAT}temp > $ONE_PKGLISTS_COMPAT #130126
  ;;
  puppy)
   echo "...it's already in the Puppy standard format."
  ;;
 esac
 #rm -f ${ONE_PKGLISTS_COMPAT}pre 2>/dev/null
 mv -f ${ONE_PKGLISTS_COMPAT}pre /tmp/ 2>/dev/null
 sort --field-separator='|' --key=1 ${ONE_PKGLISTS_COMPAT} > /tmp/$PKGLISTFILE
 mv -f /tmp/$PKGLISTFILE ${ONE_PKGLISTS_COMPAT}
done

####################################
#120825 merge updates into original db files...
if [ "$DBUPDATEFLAG" = "yes" ];then
 for ONE_PKGLISTS_COMPAT in $PKGLISTS_COMPAT
 do
  REPOFIELD="`echo -n "$ONE_PKGLISTS_COMPAT" | rev | cut -f 1 -d '-' | rev`" #ex: main
  PKGUPDATES="`echo -n "$PKGLISTS_COMPAT_UPDATES" | tr ' ' '\n' | grep "\-${REPOFIELD}$" | head -n 1`" #ex: Packages-ubuntu-precise_updates-main
  [ ! -f "$PKGUPDATES" ] && continue
  cat $PKGUPDATES > /tmp/0setup_xxx1
  cat $ONE_PKGLISTS_COMPAT >> /tmp/0setup_xxx1
  #want to discard the older package...
  sort --unique --field-separator='|' --key=2,2 /tmp/0setup_xxx1 > $ONE_PKGLISTS_COMPAT
  #...assumes pkg names remain the same, ex "firefox" (2nd field in db).
  mv -f $PKGUPDATES /tmp/$PKGUPDATES #dump -updates db file.
 done
fi

# slacko: must clear patches pkg db if it's specified in DISTRO_COMPAT_REPOS
[ -f Packages-slackware-*-patches ] && echo > Packages-slackware-*-patches

dedup() {
	str_match=$1
	shift
	for i in $@ ; do
		case "$i" in
			*"${str_match}"*) dedup_repo=$i  ;;
			*) other="$other $i" ;;
		esac
	done
	if [ "$dedup_repo" ] ; then
		cat $dedup_repo | cut -f2 -d "|" | sed '/^$/d' > z.dedup_repo
		cat $other | cut -f2 -d "|" | sed '/^$/d' > z.other
		(
		while read pkg ; do
			grep -q -m 1 "^${pkg}$" z.other && echo "\%|${pkg}|%d"
		done < z.dedup_repo
		) > sed.cmd
		sed -f sed.cmd -i "$dedup_repo"
		rm -f z.dedup_repo z.other
	fi
}

#remove duplicated pkgs in a specific pkg db (already present in other dbs)
case $DISTRO_BINARY_COMPAT in
	slackware*) dedup -slacky ${PKGLISTS_COMPAT} ;;
	debian) dedup -multimedia ${PKGLISTS_COMPAT} ;;
esac

if [ "$RUNNINGPUP" = "yes" -a "$DBmethod" = "true" ];then
 echo
 LANG=${LANGORG} echo "FINISHED"
 LANG=${LANGORG} echo "After restarting Puppy Package Manager (PPM),"
 LANG=${LANGORG} echo "it will be reading the updated database files."
 LANG=${LANGORG} echo -n "Press ENTER key to exit: "
 read enditnow
 DATE="`date +%Y%m%d `"
 echo "$DATE" > /root/.packages/db-date
 unset OSETUP_CALLED_FROM
 exit
elif [ "$RUNNINGPUP" = "yes" ];then
 kill $XPID
 Xdialog --title "$(gettext 'PPM database')" --left --msgbox "$(gettext 'PPM database was successfully completed.')" 0 100
 DATE="`date +%Y%m%d `"
 echo "$DATE" > /root/.packages/db-date
 unset OSETUP_CALLED_FROM
 exit
fi

echo "`date +%Y%m%d `" > /root/.packages/db-date
check_compat_distro_pkgs

### END ###
